package com.iss.efi.util;

import com.iss.efi.service.DataHandleService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ServiceUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ServiceUtil.applicationContext = applicationContext;
    }

    public static <T> T getBeanByName(String beanName, Class<T> clazz) {
        if (applicationContext == null) {
            throw new IllegalStateException("ApplicationContext未初始化");
        }
        return applicationContext.getBean(beanName, clazz);
    }

    public static DataHandleService getDataHandleService(String name) {
        return getBeanByName(name, DataHandleService.class);
    }
}
